﻿using Microsoft.Extensions.DependencyInjection;
using Serilog.Events;
using Serilog;
using Simple.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using SimpleSaas.Common;

namespace SimpleSaas.Web.Common
/// <summary>
/// 版 本 钢易智企2.0 V 2.0.0 钢易智企2.0
/// Copyright (c) 2023 杭州钢易科技有限公司
/// 创建人：曾东明
/// 日 期：2024
/// 描 述：
/// </summary>
{
	public static class LoggerExtension
	{
		// public static IServiceCollection AddXxlJobs(this IServiceCollection services)
		public static IServiceCollection AddSerilog(this IServiceCollection services)
		{

			string outputTemplate = App.Configuration.GetValue<string>("logOutputTemplate");//"{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}";
			string filePath = App.Configuration.GetValue<string>("logPath");// "log/file-.log";
			Console.WriteLine(filePath);
			Console.WriteLine(DateTime.Now);
			if (SimpleCheck.IsNullOrEmpty(filePath))
			{
				filePath = "log/file-.log";
			}

			if (SimpleCheck.IsNullOrEmpty(outputTemplate))
			{
				outputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}";
			}
			Log.Logger = new LoggerConfiguration()
				 .WriteTo.File(filePath,
				 fileSizeLimitBytes: 1000000,
				 rollOnFileSizeLimit: true,
				 rollingInterval: RollingInterval.Day,
				 retainedFileCountLimit: 5,
				 restrictedToMinimumLevel: LogEventLevel.Warning)
			.CreateLogger();

			services.AddLogging(loggingBuilder =>
			  loggingBuilder.AddSerilog(dispose: true) // 将Serilog添加到日志记录工厂
		  );

			return services;
		}
	}
}